package com.example.shop.common.convert;

import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.regex.Pattern;

@Component
public class LocalDateConverter implements Converter<String, LocalDate> {

    @Override
    public LocalDate convert(String source) {
        return formatterConvert(source);
    }
    public LocalDate formatterConvert(String source) {
        try {
            if(Pattern.matches("\\d{4}-\\d{2}-\\d{2}",source)){
                return  LocalDate.parse(source, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            }
            if(Pattern.matches("\\d{4}/\\d{2}/\\d{2}",source)){
                return LocalDate.parse(source, DateTimeFormatter.ofPattern("yyyy/MM/dd"));
            }
            if(Pattern.matches("\\d{4}年\\d{2}月\\d{2}日",source)){
                return LocalDate.parse(source, DateTimeFormatter.ofPattern("yyyy年MM月dd日"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
